package vn.com.vega.chacha.v2;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Appinstant.RegisterListener;
import vn.com.vega.chacha.v2.player.PlayerService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DialogConfirmPlaySong extends ActivityBase implements OnClickListener, RegisterListener{
	public static final int CONFIRM_PLAY_SONG 	= 1;
	public static final int CONFIRM_PLAY_VIDEO 	= 2;
	public static final int CONFIRM_DOWNLOAD	= 3;
	
	public static final String KEY_CONFIRM_TYPE = "Confirm type";
	public static final String KEY_CONFIRM_MESSAGE = "Confirm message";
	private int confirmType = -1;
	private String confirmMessage;
	
	private TextView mTitle;
	private TextView mMessage;
	private Button mConfirmYes;
	private Button mConfirmNo;
	private Button mConfirmRegister;
	private View confirmView;
	

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.confirm_play_song);
		mTitle = (TextView) findViewById(R.id.tv_confirm_title);
		mMessage = (TextView) findViewById(R.id.tv_confirm_message);
		mConfirmYes = (Button) findViewById(R.id.bt_confirm_yes);
		mConfirmNo = (Button) findViewById(R.id.bt_confirm_no);
		mConfirmRegister = (Button) findViewById(R.id.bt_confirm_register);
		confirmView = findViewById(R.id.view_confirm);
		
		mConfirmNo.setOnClickListener(this);
		mConfirmRegister.setOnClickListener(this);
		mConfirmYes.setOnClickListener(this);
		
		Intent intent = getIntent();
		confirmType = intent.getIntExtra(KEY_CONFIRM_TYPE, -1);
		confirmMessage = intent.getStringExtra(KEY_CONFIRM_MESSAGE);
		mMessage.setText(confirmMessage);
	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_confirm_yes:
			confirmYes();
			break;
		case R.id.bt_confirm_no:
			confirmNo();
			break;
		case R.id.bt_confirm_register:
			confirmRegister();
			break;

		default:
			break;
		}
		
	}
	
	private void confirmYes() {
		switch (confirmType) {
		case CONFIRM_DOWNLOAD:
			
			break;
		case CONFIRM_PLAY_SONG:
			Intent intent = new Intent(PlayerService.CMDPLAYNOW);
			intent.putExtra(PlayerService.CMDNAME, PlayerService.CMDPLAYNOW);
			sendBroadcast(intent);
			break;
		case CONFIRM_PLAY_VIDEO:
			Intent playVideo = new Intent(VideoPlayerScreen.CMD_PLAY_VIDEO);
			sendBroadcast(playVideo);
			break;

		default:
			break;
		}
		finish();
	}
	
	private void confirmNo() {
		switch (confirmType) {
		case CONFIRM_DOWNLOAD:
			
			break;
		case CONFIRM_PLAY_SONG:
			Intent intent = new Intent(PlayerService.CMDPLAYNOW);
			intent.putExtra(PlayerService.CMDNAME, PlayerService.CMDDONOTPLAY);
			sendBroadcast(intent);
			break;
		case CONFIRM_PLAY_VIDEO:
			Intent playVideo = new Intent(VideoPlayerScreen.CMD_DO_NOT_PLAY_VIDEO);
			sendBroadcast(playVideo);
			break;

		default:
			break;
		}
		finish();
	}
	
	private void confirmRegister() {
		confirmView.setVisibility(View.INVISIBLE);
		Handler handler = new Handler();
		Appinstant app = (Appinstant) getApplication();
		app.register(this, null, handler, false, null);
//		finish();
	}

	@Override
	public void onRegisterSuccessListener() {
		// TODO Auto-generated method stub
		confirmYes();
	}

	@Override
	public void onRegisterFailListener() {
		// TODO Auto-generated method stub
		confirmNo();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		confirmNo();
		super.onBackPressed();
	}

}
